<template>
  <van-popup
    :value="value"
    @input="$emit('input', $event)"
    @open="isReport = false"
  >
    <van-cell-group v-if="!isReport">
      <van-cell @click="disLiskArticle" icon="closed-eye">不感兴趣</van-cell>
      <van-cell is-link @click="isReport = true">反馈垃圾内容</van-cell>
      <van-cell icon="info-o">拉黑作者</van-cell>
    </van-cell-group>
    <van-cell-group v-else>
      <van-cell icon="arrow-left" @click="isReport = false">返回</van-cell>
      <!-- 渲染举报类型 -->
      <van-cell
        @click="reportArticle(report.value)"
        v-for="report in reports"
        :key="report.value"
        >{{ report.label }}</van-cell
      >
    </van-cell-group>
  </van-popup>
</template>

<script>
import { disLikeById, reportById } from '@/api/article'
// 导入举报类型的配置
import { reports } from '../../../utils/dataConfig'
export default {
  props: {
    // 接收当前要操作多文章ID
    activeArticelId: {
      type: String,
      default: ''
    },
    // 控制显示和隐藏
    value: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 是否显示上报垃圾内容
      isReport: false,
      // 举报类型数据
      reports
    }
  },

  methods: {
    // 不刚兴趣文章
    async disLiskArticle () {
      try {
        await disLikeById(this.activeArticelId)
        this.$toast.success('不感兴趣成功！')
        // 关闭弹出层
        this.$emit('input', false)
        // 通过自定义事件去修改父组件数据
        this.$emit('on-dislike')
      } catch (error) {
        // console.log(error)
        this.$toast.fail('不感兴趣失败！')
      }
    },
    // 举报文章
    async reportArticle (type) {
      try {
        await reportById(this.activeArticelId, type)
        this.$toast.success('举报成功！')
        // 关闭弹出层
        this.$emit('input', false)
        // 通过自定义事件去修改父组件数据=》删除当前举报的文章
        this.$emit('on-report')
      } catch (error) {
        this.$toast.fail('举报失败！')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.van-popup {
  width: 80%;
  border-radius: 4px;
}
</style>
